using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIEquipSkin : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIEquipSkin
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject commonEquip { protected set; get; }
            public IQIGame.Onigao.Framework.ExText nameEquipSkin { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textDes { protected set; get; }
            public UnityEngine.GameObject attributeModule { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnEquipWear { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnEquipTakeOff { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonEquip", out var __tbv0);
                this.commonEquip = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("nameEquipSkin", out var __tbv1);
                this.nameEquipSkin = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textDes", out var __tbv2);
                this.textDes = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attributeModule", out var __tbv3);
                this.attributeModule = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnEquipWear", out var __tbv4);
                this.btnEquipWear = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv5);
                this.btnClose = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnEquipTakeOff", out var __tbv6);
                this.btnEquipTakeOff = __tbv6;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIEquipSkin),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields & properties

        private UD_EquipSkin udSkin => _data as UD_EquipSkin;
        private UIN_CommonEquip equip = null;
        private ListScrollAdapter<UIN_CommonIconDesc> attrs = null;

        private bool isHas = false;
        private bool isWear = false;

        protected UIB_UIEquipSkin ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIEquipSkin();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.equip = this.InitChildNode<UIN_CommonEquip>(this.ui.commonEquip);
            this.attrs = this.InitListScroll<UIN_CommonIconDesc>(this.ui.attributeModule, this.OnAttributeChanged);
        }

        protected override void OnShow(UIBaseData data)
        {
            this.View();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnClose.onClick.AddListener(this.OnClickClose);
            this.ui.btnEquipWear.onClick.AddListener(this.OnClickWear);
            this.ui.btnEquipTakeOff.onClick.AddListener(this.OnClickTakeOff);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.equip = null;
            this.attrs = null;
        }

        #region methonds

        void OnClickClose()
        {
            this.Close();
        }

        void OnClickWear()
        {
            this.SendWearEquipSkin(this.udSkin.equipSkinCid, 0).Forget();
        }

        void OnClickTakeOff()
        {
            this.SendWearEquipSkin(0, this.udSkin.equipItem.id).Forget();
        }

        async UniTaskVoid SendWearEquipSkin(int skinCid, long equipId)
        {
            equipId = 0; //  同穿同脱
            var result = await EquipmentModule.NetCore.CS_wearSkin(skinCid, equipId);
            if (result.IsSuccess)
            {
                this.RefreshData();
                this.ViewBtn();
                this.ViewEquip();
                MsgDispatcher.Broadcast(MsgEventType.EquipSkinChanged);
            }
        }

        void RefreshData()
        {
            var equipData = this.udSkin.equipItem;
            var skinCid = this.udSkin.equipSkinCid;
            this.isHas = EquipmentModule.Instance.IsHasEquipSkin(skinCid);
            this.isWear = equipData.skinCid == skinCid;
        }

        void View()
        {
            this.RefreshData();
            this.ViewBtn();
            this.ViewEquip();
            this.ViewSkin();
            this.ViewAttribute();
        }

        void ViewEquip()
        {
            this.equip.Show(this.udSkin.equipItem);
        }

        void ViewSkin()
        {
            var cfgSkin = this.udSkin.cfgEquipSkin;
            this.ui.nameEquipSkin.text = cfgSkin.Name;
            this.ui.textDes.text = cfgSkin.Describe;
        }

        void ViewBtn()
        {
            this.ui.btnEquipWear.SetActive(this.isHas && !this.isWear);
            this.ui.btnEquipTakeOff.SetActive(this.isHas && this.isWear);
        }

        void ViewAttribute()
        {
            var cfgSkin = this.udSkin.cfgEquipSkin;
            int cntAttribute = Math.Min(cfgSkin.AttTypes.Count, cfgSkin.AttValues.Count);
            this.attrs.RefillCells(cntAttribute);
        }

        void OnAttributeChanged(UIN_CommonIconDesc item, int index)
        {
            var cfgSkin = this.udSkin.cfgEquipSkin;
            var attrCid = cfgSkin.AttTypes[index];
            var attrVal = cfgSkin.AttValues[index];
            var cfgAttribute = TableCenter.attribute.Get(attrCid);
            int nState = this.isHas ? 1 : 0;
            string strVal = APIAttributeUIUtils.GetStrAttributeValue(cfgAttribute.ShowType, attrVal);
            string desc = APIUIEquip.GetEquipSkinAddAttributeDesc(cfgAttribute, strVal);
            item.Show(nState, desc);
        }

        #endregion methonds
    }
}
